package com.huan;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HDFSIO {

    //TODO 用IO 实现HDFS的上传功能
    @Test
    public void testIOPUT() throws Exception{
        Configuration entries = new Configuration();
        FileSystem fs = FileSystem.get( new URI( "hdfs://huan01:9000" ), entries, "root" );

        //TODO 创建输入流
        FileInputStream fis = new FileInputStream( new File( "E:\\Project\\Hadoop\\HDFS\\data\\haoge.txt" ) );

        //TODO 获取输出流
        FSDataOutputStream fos = fs.create( new Path( "/huan/haoge.txt" ) );

        //TODO 流的对拷
        IOUtils.copyBytes( fis,fos, entries);

        //TODO 关闭资源
        IOUtils.closeStream( fis );
        IOUtils.closeStream( fos );
        fs.close();
        System.out.println("over");
    }

    //TODO 用IO 实现HDFS的下载功能
    @Test
    public void getFileFromHDFS() throws Exception{
        //获取对象
        Configuration entries = new Configuration();
        FileSystem fs = FileSystem.get( new URI( "hdfs://huan01:9000" ), entries, "root" );
        //获取输入流
        FSDataInputStream fis = fs.open( new Path( "/huan/sanguo/shuguo/daige.txt" ) );
        //获取输出流
        FileOutputStream fos = new FileOutputStream( new File( "E:\\Project\\Hadoop\\HDFS\\data\\daige.txt" ) );
        //流的对拷
        IOUtils.copyBytes( fis,fos,entries );
        //关闭资源
        IOUtils.closeStream( fis );
        IOUtils.closeStream( fos );
        fs.close();
    }


    @Test
    public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException {

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://huan01:9000"), configuration, "root");

        // 2 获取输入流
        FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz"));

        // 3 创建输出流
        FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.2.tar.gz.part1"));

        // 4 流的拷贝
        byte[] buf = new byte[1024];

        for(int i =0 ; i < 1024 * 128; i++){
            fis.read(buf);
            fos.write(buf);
        }

        // 5关闭资源
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
        fs.close();
    }
    @Test
    public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://huan01:9000"), configuration, "root");

        // 2 打开输入流
        FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz"));

        // 3 定位输入数据位置
        fis.seek(1024*1024*128);

        // 4 创建输出流
        FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.2.tar.gz.part2"));

        // 5 流的对拷
        IOUtils.copyBytes(fis, fos, configuration);

        // 6 关闭资源
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);

        //TODO type  hadoop-2.7.2.tar.gz.part2 >> hadoop-2.7.2.tar.gz.part1  对数据进行合并
    }



}
